import Command from "./Command";
import { Action } from "./Action";
import Handler from "./Handler";
import Hero from "../Hero";
enum Direction {
    TOP,
    BOTTOM,
    LEFT,
    RIGHT
}
export default class MoveCommand implements Command {

    private moveNode: cc.Node;
    private map: number[][];
    private dir: number;
    private pointMap: cc.Vec2[][];

    constructor(moveNode: cc.Node = null,map: number[][] = null,dir: number = -1,pointMap: cc.Vec2[][]) {
        this.moveNode = moveNode;
        this.map = map;
        this.dir = dir;
        this.pointMap = pointMap;
    }
    
    // 执行
    public exec() {
        if(this.dir === Direction.TOP) {
            this.move2Top();
        }
        if(this.dir === Direction.BOTTOM) {
            this.move2Bottom();
        }
        if(this.dir === Direction.LEFT) {
            this.move2Left();
        }
        if(this.dir === Direction.RIGHT) {
            this.move2Right();
        }
    }
    // 撤销操作
    public undo() {
        if(this.dir === Direction.TOP) {
            this.move2Bottom();
        }
        if(this.dir === Direction.BOTTOM) {
            this.move2Top();
        }
        if(this.dir === Direction.LEFT) {
            this.move2Right();
        }
        if(this.dir === Direction.RIGHT) {
            this.move2Left();
        }
    }
    // 重做
    public redo() {

    }
    private computePosition(nodeCom: Hero,colArr: number[],rowArr: number[]): void {
        colArr = nodeCom.getColArr();
        // nodeCom.setColArr[]
        let sumX: number = 0;
        let sumY: number = 0;
        for(let m = 0; m < colArr.length; m++) {
            for(let n = 0; n < rowArr.length; n++) {
                let positionItem: cc.Vec2 = this.pointMap[rowArr[n]][colArr[m]];
                if(positionItem) {
                    sumX += positionItem.x;
                    sumY += positionItem.y;
                }
            }
        }
        let x = sumX / (rowArr.length * colArr.length);
        let y = sumY / (rowArr.length * colArr.length);
        this.moveNode.x = x; 
        this.moveNode.y = y;

        // this.moveNode = null;
    }
    // 向上走
    private move2Top(): void {
        // 改变地图的模型数据
        let nodeCom: Hero = <Hero>this.moveNode.getComponent("Hero");
        let rowArr: number[] = nodeCom.getRowArr();
        let colArr: number[] = nodeCom.getColArr();
        let maxRow: number = Math.max(...rowArr);
        let minRow: number = Math.min(...rowArr);
        for(let i = 0; i < colArr.length; i++) {
            this.map[maxRow + 1][colArr[i]] = 1;
            this.map[minRow][colArr[i]] = 0;
        }
        let targetRowArr: number[] = rowArr.map((value) => {
            return value + 1;
        });
        nodeCom.setRowArr(targetRowArr);
        this.computePosition(nodeCom,colArr,targetRowArr);
    }
    // 向下走
    private move2Bottom(): void {
        let nodeCom: Hero = <Hero>this.moveNode.getComponent("Hero");
        let rowArr: number[] = nodeCom.getRowArr();
        let colArr: number[] = nodeCom.getColArr();
        let maxRow: number = Math.max(...rowArr);
        let minRow: number = Math.min(...rowArr);
        for(let i = 0; i < colArr.length; i++) {
            this.map[maxRow][colArr[i]] = 0;
            this.map[minRow - 1][colArr[i]] = 1;
        }
        let targetRowArr: number[] = rowArr.map((value) => {
            return value - 1;
        });
        nodeCom.setRowArr(targetRowArr);
        this.computePosition(nodeCom,colArr,targetRowArr);
    }
    // 向左走
    private move2Left(): void {
        // 改变地图的模型数据
        let nodeCom: Hero = <Hero>this.moveNode.getComponent("Hero");
        let rowArr: number[] = nodeCom.getRowArr();
        let colArr: number[] = nodeCom.getColArr();
        for(let i = 0; i < rowArr.length; i++) {
            for(let j = 0; j < colArr.length; j++) {
                this.map[rowArr[i]][colArr[j]] = 0;
                this.map[rowArr[i]][colArr[j] - 1] = 1;
            }
        }
        let targetcolArr: number[] = colArr.map((value) => {
            return value - 1;
        });
        nodeCom.setColArr(targetcolArr);
        this.computePosition(nodeCom,colArr,rowArr);
    }
    // 向右走
    private move2Right(): void {
        // 改变地图的模型数据
        let nodeCom: Hero = <Hero>this.moveNode.getComponent("Hero");
        let rowArr: number[] = nodeCom.getRowArr();
        let colArr: number[] = nodeCom.getColArr();
        for(let i = 0; i < rowArr.length; i++) {
            for(let j = 0; j < colArr.length; j++) {
                this.map[rowArr[i]][colArr[j]] = 0;
                this.map[rowArr[i]][colArr[j] + 1] = 1;
            }
        }
        let targetcolArr: number[] = colArr.map((value) => {
            return value + 1;
        });
        nodeCom.setColArr(targetcolArr);
        this.computePosition(nodeCom,colArr,rowArr);
    }
}